//! 错误信息定义

use std::{error::Error, fmt::Display};


#[derive(Debug)]
pub enum OrmError {
    /// 非法的字段查询方法
    IncorrectQueryMethod(String),
    /// 表无主键
    MissPkTable(String),
    /// 非法的查询值
    IncorrectQueryValue(String),
    /// 日期解析错误
    DateTimeParseFail(String),
    /// 非join字段
    NotJoinField(String),
}
impl Display for OrmError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OrmError::IncorrectQueryMethod(info) => {
                write!(f, "字段查询方法错误,非表字段或非查询方法 : {}", info)
            }
            OrmError::MissPkTable(info) => write!(f, "join表格无主键 : {}", info),
            OrmError::IncorrectQueryValue(info) => write!(f, "查询值不合法 : {}", info),
            OrmError::DateTimeParseFail(parse) => write!(f, "日期或时间解析不合法 : {}", parse.to_string()),
            OrmError::NotJoinField(info) => write!(f, "非Join字段 : {}", info),
        }
    }
}
impl Error for OrmError {}
impl OrmError {
    /// 新建非法的字段查询方法错误
    pub fn new_incorrect_query_method(info: &str) -> Self {
        Self::IncorrectQueryMethod(info.into())
    }
}
/// 带orm错误的Result
pub type OrmResult<T> = std::result::Result<T, OrmError>;
